<?php

namespace App\Models\Factory;

use App\Constants\BankConstant;
use App\Constants\CashConstant;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\AwUserAccount;
use App\Models\Orm\AwUserAccountLog;
use App\Models\Orm\User;
use App\Models\Orm\UserAccount;
use App\Models\Orm\UserAccountCash;
use App\Models\Orm\UserAccountLog;
use App\Strategies\CashStrategy;


/**
 * 现金工厂
 * Class SmsFactory
 * @package App\Models\Factory
 */
class CashFactory extends AbsModelFactory
{
    //获取用户的总金额
    public static  function getTotal($userId){

        $result = AwUserAccount::where('user_id',$userId)->first();

        return $result ?  $result-> balance : 0;

    }

    //获取用户资金记录
    public static function getUserCash($userId)
    {
        $userCash = UserAccountLog::where('user_id',$userId)->select('type','income','expend','create_at','remark','status')->get()->toArray();

        if($userCash){
            foreach ($userCash as $k => $v){
            $userCash[$k] = self::toCash($v);
            }
            return $userCash;
        }else{
            return array();
        }
    }

    //把资金转化为正负
    public static  function toCash($cash)
    {
        //转化时间
        $cash['create_at'] = str_replace('-','/',$cash['create_at']);
        $cash['create_at'] = str_replace(':','/',$cash['create_at']);


        //通过状态修改remark备注的值
        if($cash['status'] == 2)
        {
            $cash['remark'] = CashConstant::ACCOUNT_CASHOUT_REMARK_FAILD;
        }elseif($cash['status'] == 3){
            $cash['remark'] = CashConstant::ACCOUNT_CASHOUT_REMARK_SUCCESS;
        }elseif ($cash['status'] == 6){
            $cash['remark'] = CashConstant::ACCOUNT_CASHOUT_REMARK_FAILD;
        }

        if($cash['status'] == 4 || $cash['status'] == 5){
            //通过类型查看是否转为处理中
            $cash['status'] = CashStrategy::toStatus($cash['status']);
            $cash['font'] = '#FEB324';
            $cash['number'] = '+'.floatval($cash['income']).'元';
           unset($cash['income']);
           return $cash;
       }else{
            //通过类型查看是否转为处理中
            $cash['status'] = CashStrategy::toStatus($cash['status']);
           $cash['font'] = '#23BE13';
           $cash['number'] = '-'.floatval($cash['expend']).'元';
           unset($cash['expend']);
           return $cash;
       }
    }

    //提现
    public static function  getCashOut($cashOut,$userId,$nid,$account_log_id)
    {


        //生成一个提现流水号
        $data['nid'] = $nid;

        //用户支付宝账号
        $data['account'] = AliFactory::getAccount($userId);
        $data['user_id'] = $userId;

        $data['create_ip'] = Utils::ipAddress();

        $data['create_at'] = date('Y-m-d H:i:s', time());
        $data['update_at'] = date('Y-m-d H:i:s', time());
        //提现申请
        $data['status'] = BankConstant::CashOutApply;

        $data['total'] = $cashOut;
        $data['account_log_id'] = $account_log_id;

        $result = UserAccountCash::create($data);



        return $result ? true : false;


    }

    //查询用户可提现金额
    public static function getTotalCashOut($cashOut,$userId)
    {

       $userCash = UserAccount::where('user_id',$userId)->first();
        //可提现金额
        if(!$userCash)
        {
            return false;
        }

        if($cashOut > $userCash ->balance_cash)
        {
            return false;
        }

        return true;
    }


    //插入现金流水表
    public static  function createAccountLog($params)
    {

        $user = User::where('id',$params['userId'])->first();
        // 用户可提现余额
        $account = $user ->getAccount();
        $balance = $account->balance;
        // 生成资金记录
        $insert_data = [
            'user_id' => $params['userId'],
            'action' => AwUserAccountLog::ACTION_EXCHANGE,
            'order_id' => $params['order_id'],
            'type' => AwUserAccountLog::TYPE_ADD,
            'status' => AwUserAccountLog::STATUS_ACCESS,
            'money' => $params['money'],
            'balance' => $balance + $params['money'],
        ];

        $result = AwUserAccountLog::insert($insert_data);

        return $result ? true : false;

    }

    //修改用户现金账户表
    public static function updateAccountCash($params)
    {
        $user = User::where('id',$params['userId'])->first();

        // 用户可提现余额
        $account = $user ->getAccount();
        // 更新用户资金统计
        $account->balance += $params['money'];
       return $account->save();
    }
}